<?php
namespace Api\Controller;
/**
 * 请求接口测试demo
 * @author hyz
 *
 */
class TestController extends CommonController
{	
	private $option = array(
	// 			'gateway' => "http://sdk.entinfo.cn:8060/webservice.asmx/SendSMS",
			'gateway' => "http://sdk.entinfo.cn:8061/webservice.asmx/mdsmssend",
			'sn' => "SDK-WSS-010-10407",
			'pwd' => "1aad)2)c-96",
			'rrid' => '',
			'msgfmt' => '15',
			'ext' => '',
			'stime' => '',
	);
	
	public function postAction()
	{
// 		$request['sn'] = $this->option['sn'];
// 		$request['pwd'] = md5($this->option['sn'] .'+'. $this->option['pwd']);
// 		$request['rrid'] = generate_number(6,$chars='0123456789');
// 		$request['msgfmt'] = $this->option['msgfmt'];
		
// 		$request['ext'] = $this->option['ext'];
// 		$request['stime'] = $this->option['stime'];
		
// 		var_dump(strlen(http_build_query($request)));exit();
// 		var_dump(stream_get_transports());exit();

		try {
			for($j=0;$j<3;$j++)
			{
				$fp = @fsockopen("sdk2.zucp111.net", 8060, $errno, $errstr, 30);
				$timeb=time();
				if(!$fp)
				{
					$timee=time();
					$r=$timee-$timeb;
					if($r<10) sleep($r);
				}
				else
				{
				  $sockect = 1;
				  break;
				}
			  	
			}
		} catch ( \Exception $e ) {
			var_dump($e->getCode());
		}
		
		var_dump($fp);exit();
		
		var_dump(stream_get_filters());exit();
		
		var_dump(strtoupper(md5('SDK-WSS-010-104071aad)2)c-96')));
		exit();
// 		$uri = 'http://api.170.it.sibang.cn/Api/Gateway.do';
		$uri = 'http://api.170.it.sibang.cn/App/Message/send';
		
		// 测试数据
		$data = array(
				"app_id" => '20180102152936415',
				"service" => 'api.open.test',
				"method" => 'POST',
				"format" => 'xml',
				"charset" => 'utf-8',
				"sign_type" => 'md5',
				"sign" => '',
				"timestamp" => date("Y-m-d H:i:s"),
				"version" => '1.0',
				"extends" => 'array("notify_url"=> "http://api.170.it.sibang.cn/api/open/notify")',
				"data" => 'array("id"=>123,"name"=>"hyz")',
		);
		
// 		$data = $_POST;
		
		$data['sign'] = $this->getRequstSign($data);
		
		$headers = array('Content-Type: application/' . strtolower($data['format']),'charset:'.$data['charset']);
		
		if( strtolower($data['format']) == 'json' )
		{
			//TODO 数组转json
			$request = json_encode($data);
		}
		
		if( strtolower($data['format']) == 'xml' )
		{
			//TODO 数组转xml
			$request = $this->arrayToXml($data);
		}
// 		var_dump($data);exit();
		$ret = $this->responseByHttp($uri, $data['method'],$request, $headers);
		var_dump(json_decode($ret,true));
	}
	
	public function responseByHttp($uri = '',$method = '', $data = array(),$headers = array())
	{
		$ch = curl_init();
	
		curl_setopt($ch, CURLOPT_URL, $uri);
	
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
		curl_setopt($ch, CURLOPT_HEADER, false);//不返回头部信息
	
		curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
	
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 0);
		 
		if( $method == 'POST' )
		{
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
			curl_setopt($ch, CURLOPT_POST,true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		}
	
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	
		//暂不下载证书
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	
		//执行curl
		$output = curl_exec($ch);
	
		//错误提示
		if( curl_exec($ch) === false )
		{
			die(curl_error($ch));
		}
		// 检查是否有错误发生
		if( curl_errno($ch) )
		{
			echo 'Curl error: ' . curl_error($ch);
		}
	
		//释放curl句柄
		curl_close($ch);
	
		return $output;
	}
	
}